package com.wtx.chapter04;

/**
 * @description: 插入排序
 * @author: wtx
 * @createDate: 2020/6/10
 */
public class InsertionSort {

    public static void sort(int[] array){

        int v = 0;
        int j = 0;
        // 每次循环得到array[0..i]的有序数组
        for (int i = 0; i < array.length; i++) {

            v = array[i];   //待插入的元素
            j = i - 1; //array[0..j]为有序数组
            while (j>=0 && array[j]>v){
                array[j+1] = array[j];
                j--;
                array[j+1] = v;
            }
        }
    }

    public static void main(String[] args) {
        int[] arr = {10,20,5,8};
        InsertionSort.sort(arr);

        for (int i : arr) {
            System.out.println(i);
        }
    }
}
